﻿using System.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JUtil;
using JUtil.CodeGenerator;

namespace CodeGenerator
{
    public class MasterViewAspxFile : CodeGenBase
    {
        private readonly string NAMESPACE;
        private readonly string CLASS;
        private readonly string DAL_CLASS;
        private readonly string BLL_CLASS;
        private readonly List<DalProperty> lstDalField;

        public MasterViewAspxFile(
            DbSetting DbSetting,
            CSharpFileSetting CSharpFileSetting,
            RelatedSql RelatedSql)
        {
            NAMESPACE = CSharpFileSetting.NAMESPACE;
            CLASS = CSharpFileSetting.ASPX_CLASS;
            DAL_CLASS = CSharpFileSetting.DAL_CLASS;
            BLL_CLASS = CSharpFileSetting.BLL_CLASS;

            IDbExtraOp Database = new Database(DbSetting.DbProvider, DbSetting.ConnectionString);
            this.lstDalField = GetDalFields(RelatedSql.GetSelectSQLNoOrderBy, Database);
        }

        private List<DalProperty> GetDalFields(
            string SelectSQL,
            IDbExtraOp Database)
        {
            List<DalProperty> result = new List<DalProperty>();

            List<FieldSchema> lstFieldSchema = new List<FieldSchema>();

            TableSchema ts = TableSchema.GetBySql(Database, SelectSQL);
            lstFieldSchema.AddRange(ts.Fields);

            foreach (FieldSchema field in lstFieldSchema)
            {
                result.Add(new DalProperty
                {
                    Type = field.DataType,
                    Name = field.ColumnName,
                    Comment = field.ColumnName
                });
            }

            return result;
        }

        private string TemplateFields
        {
            get
            {
                StringBuilder properties = new StringBuilder();

                string TemplateField = GetTemplate("TemplateField");

                foreach (DalProperty info in lstDalField)
                {
                    try
                    {
                        Hashtable variables = new Hashtable();
                        variables["Comment"] = info.Comment;
                        variables["Name"] = info.Name;

                        string property = ReplaceBlock(TemplateField, variables);

                        properties.AppendFormat("{0}\n", property);
                    }
                    catch (Exception ex)
                    {
                        Log.ReportError(ex);
                    }
                }

                return properties.ToString();
            }
        }

        protected override void AssignExportTemplate(Hashtable Variables)
        {
            Variables["NAMESPACE"] = NAMESPACE;
            Variables["CLASS"] = CLASS;
            Variables["DAL_CLASS"] = DAL_CLASS;
            Variables["BLL_CLASS"] = BLL_CLASS;
            Variables["TemplateFields"] = TemplateFields;
        }

        protected override string GetExportTemplate()
        {
            return GetTemplate("ExportTemplate");
        }
    }
}
